import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { saveGoods, queryGoodsCategory, getGoodsGroup, addGoodsGroup, getGoodsDetail, getQrcode, getAddLinkGoods, postFileUpload, postFileVideoUpload } from '../services/api';

export default {
  namespace: 'addGoods',

  state: {
    category: [],
    group: [],
    goods : {
      link_info : {}
    }
  },

  effects: {
    *getGoodsDetail({ payload }, { call, put }){
      const res = yield call(getGoodsDetail, payload);
      if(res){
        yield put({
          type: 'save',
          payload : {goods : res}
        });
      }
    },
    *getCategory({ payload }, { call, put }){
      const res = yield call(queryGoodsCategory, payload);
      if(res){
        yield put({
          type: 'save',
          payload : {category : res}
        });
      }
    },
    *getGroup({ payload }, { call, put }){
      const res = yield call(getGoodsGroup, payload);
      if(res){
        yield put({
          type: 'save',
          payload : {group : res}
        });
      }
    },
    *saveGroup({ payload }, { call, put }){
      const res = yield call(addGoodsGroup, payload);
      if(res){
        yield put({
          type: 'addGroup',
          payload : res
        });
        message.success('新增成功！');
      }
    },
    *saveGoods({ payload }, { call, put }) {
      const res = yield call(saveGoods, payload);
      if(res){
        yield put({
          type: 'saveStepFormData',
          payload,
        });
        message.success(payload.id ? '修改成功' : '新增成功！');
        //yield put(routerRedux.push(`/goods/goods-add?id=${res.id}`));
        setTimeout(function(){
          window.location.reload(true);
        },2000);
      }
    },
    *saveVideo({ payload }, { call, put }) {
      yield put({
        type : 'saveAddVideo',
        payload : payload,
      });
    },
    *createQrcode({ payload }, { call, put }) {
      const res = yield call(getQrcode, payload);
      if(res){
        // yield put({
        //   type: 'saveStepFormData',
        //   payload,
        // });
        message.success('成功');
        window.open(res.url);
        //yield put(routerRedux.push(`/goods/goods-add?id=${res.id}`));
      }
    },
    *getLinkGoods({ payload }, { call, put }){
      const res = yield call(getAddLinkGoods, payload);
      if(res){
        yield put({
          type : 'save',
          payload : {
            link_goods : res.link_goods
          },
        });
      }
    },
    *upload({ payload }, { call, put }){
      const res = yield call(postFileUpload, payload);
      if(res){
        // yield put({
        //   type: 'save',
        //   payload : {goods : res}
        // });
        return res;
      }
    },
    *uploadVideo({ payload }, { call, put }){
      const res = yield call(postFileVideoUpload, payload);
      if(res){
        // yield put({
        //   type: 'save',
        //   payload : {goods : res}
        // });
        return res;
      }
    },
  },

  reducers: {
    saveStepFormData(state, { payload }) {
      return {
        ...state,
      };
    },
    addGroup(state, {payload}){
      let group = [payload, ...state.group];
      return {
        ...state,
        group: group
      }
    },
    save(state, {payload}){
      return {
        ...state,
        ...payload
      }
    },
    saveAddVideo(state, {payload}){
      let goods = {...state.goods};
      goods.video = payload.url;
      return {
        ...state,
        goods,
      }
    },
    clear() {
      return {
        category: [],
        group: [],
        goods : {
          link_info : {}
        }
      }
    }
  },
};
